<?php

namespace App\Repositories\Api;

use App\Models\Member;
use App\Models\PipeiMapping;

class PipeiMappingRepository
{
    /**
     * 获取匹配数据
     * @param $uid
     */
    public function getPipeiMapping($uid = null, $size = 5)
    {
        //获取当天数据
        if (!intval($uid)) return null;
        $filter['status'] = 1;
        $ppUids = PipeiMapping::query()->where(['uid' => intval($uid), 'status' => 1])->limit($size)->orderBy("date", "desc")->pluck("pipei_uid");
        $gender = Member::query()->where(['id' => $uid])->value("gender");

        $memberRep = new MemberRepository();
        if (empty($ppUids)) {  //无匹配直接获取
            return $memberRep->getPages(['gender' => $gender == 'F' ? 'M' : 'F'], 1, $size);
        } else {
            $filter['id'] = ['in', $ppUids];
        }

        $ps = Member::query()->where(['status' => 1])->whereIn("id", $ppUids)
            ->orderBy("last_login_time", "desc")->orderBy("reg_time", "desc")
            ->get();

        if (empty($ps)) {
            return $memberRep->getPages(['gender' => $gender == 'F' ? 'M' : 'F'], 1, $size);
        }

        return $ps;
    }


}
